<?php
/**
 * 秒杀商品
 * User: ws
 * Date: 2018/9/30
 * Time: 9:32
 */

namespace app\admin\controller;
use think\db;
use app\admin\model\GoodsSeckill as GSModeL;

class Gseckill  extends Init
{
    function index(){
        if ($this->request->isAjax()) {
            $key = input('key')?input('key'):'';
            $where['s.name|g.price'] = array('like','%'.trim($key).'%');

            $list = Db::table('je_goods_seckill')->alias('g')->join('je_goods s','g.goods_id = s.goods_id')->field('s.name as sname,g.*')->where($where)->order('seckill_id desc')->paginate(input('limit'))->toArray();

            foreach ($list['data'] as $key=>$itme){
                $list['data'][$key]['add_time'] = date('Y-m-d H:i:s',$itme['add_time']);
                $list['data'][$key]['spec_item'] = db('spec_goods_price')->where(['price_key'=>$itme['spec_item'],'goods_id'=>$itme['goods_id']])->value('price_key_name');
            }

            return json($result = ['code'=>0,'msg'=>'获取成功','data'=>$list['data'],'count'=>$list['total']]);
        }
        return view('list');
    }

    function add(){
        if(request()->isPost()){
            $data = $this->request->post();

            if(empty($data['start_time']) || empty($data['end_time'])){
                return json(array('code'=>0,'msg'=>'添加失败,秒杀时间不能为空'));
            }
            if (!GSModeL::create($data)) {
                return json(array('code'=>0,'msg'=>'添加失败'));
            }
            return json(array('code'=>200,'msg'=>'添加成功'));
        }

        $goods = db('goods')->field('name,goods_id')->select();
        return view('add',['goods'=>$goods]);
    }


    function edit(){
        if(request()->isPost()){
            $data = $this->request->post();

            if(empty($data['start_time']) || empty($data['end_time'])){
                return json(array('code'=>0,'msg'=>'添加失败,秒杀时间不能为空'));
            }

            if (!GSModeL::update($data)) {
                return json(array('code'=>0,'msg'=>'修改失败'));
            }
            return json(array('code'=>200,'msg'=>'修改成功'));
        }

        $list = GSModeL::where('seckill_id', input('id'))->find()->toArray();
        $goods = db('goods')->field('name,goods_id')->select();
        return view('edit',['list'=>$list,'goods'=>$goods]);
    }

    function del(){
        $id = input('param.id');
        $data = Db::name('goods_success_killed')->where('seckill_id',$id)->find();
        if($data){
            return json(array('code'=>0,'msg'=>'删除失败，已被秒杀不能被删除'));
        }
        if (!GSModeL::destroy(['seckill_id' => $id])) {
            return json(array('code'=>0,'msg'=>'删除失败'));
        }
        return json(array('code'=>200,'msg'=>'删除成功'));
    }

    function index2(){
        if ($this->request->isAjax()) {
            $key = input('key')?input('key'):'';
            $where['s.name|u.name'] = array('like','%'.trim($key).'%');
            $list = Db::table('je_goods_success_killed')->alias('k')->join('je_goods_seckill g','k.seckill_id = g.seckill_id')->join('je_goods s','g.goods_id = s.goods_id')->join('je_user u','k.user_id = u.user_id')->field('s.name as sname,s.goods_id,u.name as uname,g.spec_item,k.*')->where($where)->order('seckill_id desc')->paginate(input('limit'))->toArray();
            foreach ($list['data'] as $key=>$itme){
                $list['data'][$key]['create_time'] = date('Y-m-d H:i:s',$itme['create_time']);
                $list['data'][$key]['spec_item'] = db('spec_goods_price')->where(['price_key'=>$itme['spec_item'],'goods_id'=>$itme['goods_id']])->value('price_key_name');
            }
            return json($result = ['code'=>0,'msg'=>'获取成功','data'=>$list['data'],'count'=>$list['total']]);
        }
        return view('list2');
    }
}